package com.hunan.api.catv.service.biz.system;


import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.po.SystemRoleAuth;
import com.hunan.api.catv.entity.vo.SystemRoleAuthVo;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.impl.system.SystemAuthService;
import com.hunan.api.catv.service.impl.system.SystemRoleAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.LinkedList;
import java.util.List;

@Service
public class SystemRoleAuthBiz extends ResultService {

    @Autowired
    SystemAuthService systemAuthService;

    @Autowired
    SystemRoleAuthService systemRoleAuthService;

    public List<Integer> list(Integer sysRoleId) {
        List<SystemRoleAuthVo> ralist = systemRoleAuthService.getList(sysRoleId);
        List<Integer> list = new LinkedList<Integer>();
        if (!ObjectUtils.isEmpty(ralist)) {
            for (SystemRoleAuthVo systemRoleAuthVo : ralist) {
                list.add(systemRoleAuthVo.getSysAuthId());
            }
        }
        return list;
    }

    public Integer batchEdit(Integer[] ids, Integer sysRoleId) {
        ServiceResult serviceResult = systemRoleAuthService.delete(sysRoleId);
        if (serviceResult.getStatus() && !ObjectUtils.isEmpty(ids)) {
            List<SystemRoleAuth> list = new LinkedList<SystemRoleAuth>();
            for (Integer authId : ids) {
                SystemRoleAuth systemRoleAuth = new SystemRoleAuth();
                systemRoleAuth.setSysAuthId(authId);
                systemRoleAuth.setSysRoleId(sysRoleId);
                list.add(systemRoleAuth);
            }
            if (ObjectUtils.isEmpty(list)) {
                return 0;
            }
            ServiceResult sr = systemRoleAuthService.batchAdd(list);
            return (Integer) sr.getData();
        }
        return 0;
    }
}